package klassen;

import java.sql.*;


public class Wuerfel {
	
	public Login login;
	Connection conn;
	public int fieldid;
	public int playerid;
	
public Wuerfel(){
	
}
	
public int wuerfeln (){
	 int zufallszahl = 1+ (int) (Math.random()*6);
	 System.out.println(zufallszahl);
     return zufallszahl;
}

public int newPosition (int a, int b, String user) {	
try {
int sum = a + b;
DbVerbindung dbverbindung = new DbVerbindung();
conn = dbverbindung.getConnection();
PreparedStatement stmt =null;
PreparedStatement stmt2 =null;
//PreparedStatement stmt2 =null;

String sql ="SELECT ip.fieldid, p.playerid FROM monopolydb.player p "
	+ "INNER JOIN monopolydb.isplaying ip ON p.playerid = ip.playerid "
	+ "WHERE p.nickname = ?";
stmt = (PreparedStatement) conn.prepareStatement(sql);
stmt.setString(1, user);
ResultSet rs = stmt.executeQuery();
while (rs.next()){
	fieldid = rs.getInt("fieldid");
	playerid = rs.getInt("playerid");
	}

fieldid= fieldid+sum;
if (fieldid > 40){
    fieldid = fieldid - 40;
    }
    else{}

String sql2 = "Update monopolydb.isplaying SET fieldid = ? "
		+ "WHERE playerid = ?";
stmt2 =(PreparedStatement) conn.prepareStatement(sql2);
stmt2.setInt(1, fieldid);
stmt2.setInt(2, playerid);
int anzahl = stmt2.executeUpdate();
//String sql2 = "UPDATE monopolydb.isplaying SET "

System.out.println(user);
System.out.println(sum);
System.out.println(fieldid);
System.out.println(playerid);

stmt.close();
stmt2.close();
rs.close();
conn.close();

 }
catch (SQLException ex) {
	
}


return fieldid;




}

}


